package com.lecture.java8.node;

import java.util.ArrayList;
import java.util.List;

public class RecursionTest {

    public static void main(String[] args) {
        Node n0 = new Node();
        n0.setId(90L);
        n0.setParentId(0L);
        n0.setNodeName("父节点");

        Node n1 = new Node();
        n1.setId(1L);
        n1.setParentId(90L);
        n1.setNodeName("节点1");

        Node n2 = new Node();
        n2.setId(2L);
        n2.setParentId(90L);
        n2.setNodeName("节点2");

        Node n3 = new Node();
        n3.setId(3L);
        n3.setParentId(1L);
        n3.setNodeName("节点1-1");

        Node n4 = new Node();
        n4.setId(4L);
        n4.setParentId(1L);
        n4.setNodeName("节点1-2");

        Node n5 = new Node();
        n5.setId(5L);
        n5.setParentId(1L);
        n5.setNodeName("节点1-3");

        Node n6 = new Node();
        n6.setId(6L);
        n6.setParentId(2L);
        n6.setNodeName("节点2-1");

        Node n7 = new Node();
        n7.setId(7L);
        n7.setParentId(6L);
        n7.setNodeName("节点2-1-1");

        Node n8 = new Node();
        n8.setId(8L);
        n8.setParentId(6L);
        n8.setNodeName("节点2-1-2");

        Node n9 = new Node();
        n9.setId(9L);
        n9.setParentId(3L);
        n9.setNodeName("节点1-1-1");

        Node n10 = new Node();
        n10.setId(10L);
        n10.setParentId(3L);
        n10.setNodeName("节点1-1-2");

        Node n11 = new Node();
        n11.setId(11L);
        n11.setParentId(3L);
        n11.setNodeName("节点1-1-3");

        Node n12 = new Node();
        n12.setId(12L);
        n12.setParentId(2L);
        n12.setNodeName("节点2-2");

        Node n13 = new Node();
        n13.setId(13L);
        n13.setParentId(3L);
        n13.setNodeName("节点1-1-4");

        Node n14 = new Node();
        n14.setId(14L);
        n14.setParentId(12L);
        n14.setNodeName("节点2-2-2");

        Node n15 = new Node();
        n15.setId(15L);
        n15.setParentId(12L);
        n15.setNodeName("节点2-2-1");

        Node n16 = new Node();
        n16.setId(16L);
        n16.setParentId(15L);
        n16.setNodeName("节点2-2-1-1");

        List<Node> nodeList = new ArrayList<>();
        nodeList.add(n0);
        nodeList.add(n1);
        nodeList.add(n2);
        nodeList.add(n3);
        nodeList.add(n4);
        nodeList.add(n5);
        nodeList.add(n6);
        nodeList.add(n7);
        nodeList.add(n8);
        nodeList.add(n9);
        nodeList.add(n10);
        nodeList.add(n11);
        nodeList.add(n12);
        nodeList.add(n13);
        nodeList.add(n14);
        nodeList.add(n15);
        nodeList.add(n16);
        List<Node> roots = ForestNodeMerger.merge(nodeList);
        getValue(roots.get(0));
        System.out.println(roots);
    }

    public static void getValue(Node node) {

        if (node.getChildren() == null || node.getChildren().size() == 0) {
            //如果没有子节点 默认设置value=1
            node.setValue(1);
            return;
        }
        //遍历当前节点的所有子节点
        int value = 0;
        for (INode child : node.getChildren()) {
            if (child.getChildren() != null || child.getChildren().size() > 0) {
                getValue((Node) child);
            }
            //走到这里所有的子节点的value已经赋值了
            Node n = (Node) child;
            //记录每个子节点的值 并且相加
            value += n.getValue();
        }

        node.setValue(value);
    }
}
